package com.example.cat.chinagirl918.Server;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;

import com.example.cat.chinagirl918.R;
import com.example.cat.chinagirl918.WidgetManger.NewAppWidget;


import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by cat on 2018/4/26.
 */

public class UpdateWidgetService extends Service {
    private Timer timer;
    private TimerTask task;

    public UpdateWidgetService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                //    Log.d("CatFlag", "Run" + new Random().nextInt());
                ComponentName componentName = new
                        ComponentName(UpdateWidgetService.this
                        , NewAppWidget.class);
                RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.new_app_widget);
                remoteViews.setTextColor(R.id.timePicker, Color.BLUE);

                //由AppWidgetManager处理Wiget。
                AppWidgetManager awm = AppWidgetManager.getInstance(getApplicationContext());
                awm.updateAppWidget(componentName, remoteViews);
            }
        };
        timer.schedule(task, 0, 1000);
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
        task.cancel();
        timer = null;
        task = null;
    }
}
